﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WebCamera
{
    public class BrowserPageFactoryO365 : BrowserPageFactory
    {
        public new static List<BrowserPageProxy> GetAllPages()
        {
            BrowserWindow window;
            BrowserPageProxy proxy;
            List<BrowserPageProxy> list = new List<BrowserPageProxy>();
            foreach (IntPtr ptr in GetMainWindowHwnd("Microsoft Internet Explorer"))
            {
                window = new BrowserWindow(ptr, BrowserType.IE6);
                window.Edge = Configration.IE6Edge;
                foreach (IntPtr ptr2 in IEPage.GetPageHandlers(ptr))
                {
                    IEO365Page page = new IEO365Page(ptr2, window);
                    proxy = new BrowserPageProxy(page);
                    list.Add(proxy);
                }
            }
            foreach (IntPtr ptr in GetMainWindowHwnd("Windows Internet Explorer"))
            {
                window = new BrowserWindow(ptr, BrowserType.IE7);
                window.Edge = Configration.IE7Edge;
                foreach (IntPtr ptr2 in IEPage.GetPageHandlers(ptr))
                {
                    IEO365Page page2 = new IEO365Page(ptr2, window);
                    proxy = new BrowserPageProxy(page2);
                    list.Add(proxy);
                }
            }
            foreach (IntPtr ptr in GetMainWindowHwnd("Mozilla Firefox"))
            {
                window = new BrowserWindow(ptr, BrowserType.FF3);
                window.Edge = Configration.FF3Edge;
                foreach (IntPtr ptr2 in FFPage.GetPageHandlers(ptr))
                {
                    FFPage page3 = new FFPage(ptr2, window);
                    proxy = new BrowserPageProxy(page3);
                    list.Add(proxy);
                }
            }
            return list;
        }

        public new static List<BrowserPageProxy> GetAllPages(string url)
        {
            List<BrowserPageProxy> list = new List<BrowserPageProxy>();
            foreach (BrowserPageProxy proxy in GetAllPages())
            {
                if (proxy.Url == url)
                {
                    list.Add(proxy);
                }
            }
            return list;
        }

        #region private mathods
        private static List<IntPtr> GetMainWindowHwnd(string title)
        {
            Regex regex = new Regex(@"\w*" + title + @"\w*");
            return GetMainWindowHwnd(regex);
        }

        private static List<IntPtr> GetMainWindowHwnd(Regex regex)
        {
            List<IntPtr> list = new List<IntPtr>();
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (regex.Match(process.MainWindowTitle).Success)
                {
                    list.Add(process.MainWindowHandle);
                }
            }
            return list;
        }
        #endregion
    }
}
